package com.whu.waterworks.action;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.opensymphony.xwork2.ActionSupport;
import com.whu.waterworks.model.Alert;
import com.whu.waterworks.model.AlertRecord;
import com.whu.waterworks.model.AlertRecordId;
import com.whu.waterworks.service.AlertRecordService;
import com.whu.waterworks.service.AlertService;
import com.whu.waterworks.service.WaterPlantService;
import com.whu.waterworks.util.CodeTransformationUtil;

/**
 * @author cai
 * @date 2016-3-26
 * 
 */

public class LinkToAlert extends ActionSupport {
	private static final long serialVersionUID = 3905635347601311317L;
	private String alertType;
	private AlertService alertService;
	private List<Alert> alerts;
	private Alert alert; // 预警链接过来的
	private AlertRecordService alertRecordService;
	private String stationnum;
	private WaterPlantService waterPlantService;
	private String waterPlantName;
	private String recordTime;

	private String disposealertname;
	private Integer alertId;
	private AlertRecord alertRecord;

	private String result;

	@Override
	public String execute() throws Exception {
		alerts = alertService.getAll();
		//去掉前后空格
		alertType =alertType.trim();
		alert = alertService.findByAlertType(alertType);

		return SUCCESS;
	}

	public String disposeAlertRecordInit() throws Exception {
		alerts = alertService.getAll();
		alert = alertService.findByAlertType(alertType);
		alertId = alert.getAlertId();
		AlertRecordId alertRecordId = new AlertRecordId();

		alertRecordId.setAlertId(alertId);
		alertRecordId.setTime(recordTime);
		alertRecord = alertRecordService.findById(alertRecordId);
		// AlertRecord alertRecord =alertRecordService.get
		try {
			waterPlantName = waterPlantService.getWaterPlantById(
					CodeTransformationUtil.getWaterplantCode(stationnum))
					.getWaterPlantName();
		} catch (Exception e) {
			waterPlantName = "未找到位置";
			System.out.println(e.toString());
		}
		return SUCCESS;
	}

	public String updateAlertRecord() {

		AlertRecordId alertRecordId = new AlertRecordId();

		alertRecordId.setAlertId(alertId);
		alertRecordId.setTime(recordTime);
		alertRecord = alertRecordService.findById(alertRecordId);

		// 获取当前登录的用户
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		if (auth.getPrincipal() instanceof UserDetails) {
			alertRecord.setUsername(((UserDetails) auth.getPrincipal())
					.getUsername());
		}
		if (alertRecord.getFlag()==null || alertRecord.getFlag().equals("1") == false) {
			alertRecord.setFlag("1");
			if (disposealertname!=null) {
				alertRecord.setDisposealertname(disposealertname);
			}
			if (alertRecordService.updateRecord(alertRecord).equals("success") == true) {
				result = SUCCESS;
			} else {
				result = ERROR;
			}
			

		} else {
			result = "hasDisposed";
		}

		return SUCCESS;
	}

	public String getAlertType() {
		return alertType;
	}

	public void setAlertType(String alertType) {
		this.alertType = alertType;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public AlertService getAlertService() {
		return alertService;
	}

	public void setAlertService(AlertService alertService) {
		this.alertService = alertService;
	}

	public List<Alert> getAlerts() {
		return alerts;
	}

	public void setAlerts(List<Alert> alerts) {
		this.alerts = alerts;
	}

	public Alert getAlert() {
		return alert;
	}

	public void setAlert(Alert alert) {
		this.alert = alert;
	}

	public AlertRecordService getAlertRecordService() {
		return alertRecordService;
	}

	public void setAlertRecordService(AlertRecordService alertRecordService) {
		this.alertRecordService = alertRecordService;
	}

	public String getStationnum() {
		return stationnum;
	}

	public void setStationnum(String stationnum) {
		this.stationnum = stationnum;
	}

	public WaterPlantService getWaterPlantService() {
		return waterPlantService;
	}

	public void setWaterPlantService(WaterPlantService waterPlantService) {
		this.waterPlantService = waterPlantService;
	}

	public String getWaterPlantName() {
		return waterPlantName;
	}

	public void setWaterPlantName(String waterPlantName) {
		this.waterPlantName = waterPlantName;
	}

	public String getRecordTime() {
		return recordTime;
	}

	public void setRecordTime(String recordTime) {
		this.recordTime = recordTime;
	}

	public String getDisposealertname() {
		return disposealertname;
	}

	public void setDisposealertname(String disposealertname) {
		this.disposealertname = disposealertname;
	}

	public AlertRecord getAlertRecord() {
		return alertRecord;
	}

	public void setAlertRecord(AlertRecord alertRecord) {
		this.alertRecord = alertRecord;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Integer getAlertId() {
		return alertId;
	}

	public void setAlertId(Integer alertId) {
		this.alertId = alertId;
	}

}
